<div class="page-container">
    <div class="info-wrap">
        <div class="info-row">
            <!--Last 500 mined blocks start-->
            {{ function capitalizeFirstLetter(t){return t.charAt(0).toUpperCase()+t.slice(1)} }}
            {{ function readableDate(a){ return new Date(parseInt(a)).toString(); } }}
            {{ for(var pool in it.stats.pools) { }}
            {{ var blockscomb = new Array; }}
            <div class="desktop-100 faq-wrap">
                {{ for(var pool in it.stats.pools) { }}
                <div class="home-title">
                    <h2 class="blocksT6">Last mined block</h2>
                </div>
                <div class="blocks-content">
                    <table class="home-table" id="blocksTable">
                        <thead>
                        <tr class="home-tr">
                            <th class="home-td th-block blocksT1">Block height</th>
                            <th class="home-td th-hash blocksT2">Hash value</th>
                            <th class="home-td th-time blocksT3">Time</th>
                            <th class="home-td th-luck blocksT4">Luck</th>
                            <th class="home-td th-miner blocksT5">Miner</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
                {{ } }}
                {{ } }}
            </div>
            <!--Last 500 mined blocks end-->
        </div>
    </div>
</div>
<script>
    var poolHashrateData;
    $(function() {
        statsSource.addEventListener('message', function (e) {
            var stats = JSON.parse(e.data);
            poolHashrateData = [];
            for (var pool in stats.pools) {
                poolHashrateData.push({
                    key: pool,
                    values: stats.pools[pool].hashrate
                });
                $('#statsMiners' + pool).text(stats.pools[pool].minerCount);
                $('#statsWorkers' + pool).text(stats.pools[pool].workerCount);
                $('#statsHashrate' + pool).text(stats.pools[pool].hashrateString);
                $('#statsHashrateAvg').text(getReadableHashRateString(calculateAverageHashrate(pool)));
                $('#statsLuckDays' + pool).text(stats.pools[pool].luckDays);
                $('#statsValidBlocks' + pool).text(stats.pools[pool].poolStats.validBlocks);
                $('#statsTotalPaid' + pool).text((parseFloat(stats.pools[pool].poolStats.totalPaid)).toFixed(8));
                $('#statsNetworkBlocks' + pool).text(stats.pools[pool].poolStats.networkBlocks);
                $('#statsNetworkDiff' + pool).text((parseFloat(stats.pools[pool].poolStats.networkDiff)).toFixed(5));
                $('#statsNetworkSols' + pool).text(getReadableNetworkHashRateString(stats.pools[pool].poolStats.networkSols));
                $('#statsNetworkConnections' + pool).text(stats.pools[pool].poolStats.networkConnections);
            }
        });


        function calculateAverageHashrate(pool) {
            var count = 0;
            var total = 1;
            var avg = 0;
            for (var i = 0; i < poolHashrateData.length; i++) {
                count = 0;
                for (var ii = 0; ii < poolHashrateData[i].values.length; ii++) {
                    if (pool == null || poolHashrateData[i].key === pool) {
                        count++;
                        avg += parseFloat(poolHashrateData[i].values[ii][1]);
                    }
                }
                if (count > total)
                    total = count;
            }
            avg = avg / total;
            return avg;
        }

        function getReadableHashRateString(hashrate){
            var i = 0;
            var byteUnits = [' H/s', ' KH/s', ' MH/s', ' GH/s', ' TH/s', ' PH/s' ];
            while (hashrate >= 1024){
                hashrate = hashrate / 1024;
                i++;
            }
            return hashrate.toFixed(2) + byteUnits[i];
        }

        function getReadableNetworkHashRateString(hashrate){
            var i = 0;
        var byteUnits = [' H/s', ' KH/s', ' MH/s', ' GH/s', ' TH/s', ' PH/s' ];
        while (hashrate > 1024){
            hashrate = hashrate / 1024;
            i++;
        }
        return hashrate.toFixed(2) + byteUnits[i];

        }

        var groupedByFinder = {};
        var data = [];
        var blockscomb = [];

        for (var i=0; i < blockscomb.length; i++) {
            finder=blockscomb[i][3]; // if other doesn 't already have a property for the current letter
            // create it and assign it to a new empty array
            if (!(finder in groupedByFinder))
                groupedByFinder[finder] = [];

            groupedByFinder[finder].push(blockscomb[i]);
        }

        Object.keys(groupedByFinder).forEach(function(i) {
            var obj = {};
            obj.label = i
            obj.value = groupedByFinder[i].length
            data.push(obj)
        });

        var _miner = "{{=String(it.stats.address).split(".")[0]}}";
        function getWorkerNameFromAddress(w) {
            var worker = w;
            if (w.split(".").length > 1) {
                worker = w.split(".")[1];
                if (worker == null || worker.length < 1) {
                    worker = "noname";
                }
            } else {
                worker = "noname";
            }
            return worker;
        }

        //Datepicker
        function formatDateTime(timeStamp) {
            var date = new Date();
            date.setTime(timeStamp);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            h = h < 10 ? ('0' + h) : h;
            var minute = date.getMinutes();
            var second = date.getSeconds();
            minute = minute < 10 ? ('0' + minute) : minute;
            second = second < 10 ? ('0' + second) : second;
            return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
        };

        var poolData = {{=JSON.stringify(it.stats.pools)}};
        var blockHtml = '';
        for (var pool in poolData) {
            for (var b in poolData[pool].pending.blocks){
                var block = poolData[pool].pending.blocks[b].split(":");
                blockHtml +='<tr class="home-tr">' +
                    '<td class="home-td td-block">' +
                    '<a href="http://testnet-explorer.ulord.one/block/'+ block[0]+ '" target="_blank">' + block[2] +'</a>' +
                    '</td>' +
                    '<td class="home-td td-hash">' +
                    '<a href="http://testnet-explorer.ulord.one/block/'+ block[0] +'" target="_blank">' + block[0] +'</a>' +
                    '<label>'+ block[0] + '</label>' +
                    '</td>' +
                    '<td class="home-td td-time">' +
                    '<small>'+ formatDateTime(block[4]=='x'?block[5]:block[4]) +'</small>' +
                    '</td>' +
                    '<td class="home-td td-luck">' +
                    '<div>' +
                    '<span class="badge-green" id="statsLuckDays"'+ pool+'>'+ poolData[pool].luckDays +'</span>' +
                    '</div>' +
                    '</td>' +
                    '<td class="home-td td-miner">' +
                    '<a href="/miners/'+ block[3].split('.')[0] +'">'+ block[3] +'</a>' +
                    '</td>' +
                    '</tr> '
            }
            $('#blocksTable tbody').html(blockHtml);
        }

    });


</script>



